package com.zhouzj.task.zookeeper.provider.service.user;

import com.zhouzj.task.zookeeper.common.annotions.RpcService;
import com.zhouzj.task.zookeeper.export.dto.User;
import com.zhouzj.task.zookeeper.export.api.user.IUserService;
import org.jboss.netty.util.internal.ConcurrentHashMap;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Random;

/**
 * @Author: zhouzhijun
 * @Date: 2021/7/6 11:14
 */
@RpcService
@Service
public class UserServiceImpl implements IUserService {

    static Map<Integer, User> users = new ConcurrentHashMap<>();

    @Override
    public User user(Integer id) {
        User user = User.builder()
                .id(1)
                .age(11)
                .name("张三").build();
        users.put(user.getId(), user);

        user = User.builder()
                .id(2)
                .age(12)
                .name("张三2").build();
        users.put(user.getId(), user);

        user = User.builder()
                .id(3)
                .age(13)
                .name("张三3").build();
        users.put(user.getId(), user);

        return users.get(id);
    }

    @Override
    public String sayHello(String word) {
        Random random = new Random();
        int millis = random.nextInt(200);
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("接到请求:" + word + ", 服务端随机sleep:" + millis + "ms");
        return word;
    }
}
